import apiList from 'apiList.js' //  引入apiList.js文件

const apiRequest = (url, method, data, header) => { //接收所需要的参数，如果不够还可以自己自定义参数
    let promise = new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data ? data : {},
            method: method,
            header: header ? header : {
                "Content-Type": "application/json"
            },
            success: function (res) {
                if (res.data.status == 401) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500,
                        complete: function (res) {
                            wx.setStorageSync('islogin', false)
                            setTimeout(function () {
                                wx.switchTab({
                                    url: '/pages/mine/mine',
                                })
                            }, 1500)
                            return
                        }
                    })
                }
                //接口调用成功
                resolve(res); //根据业务需要resolve接口返回的json的数据
            },
            fail: function (res) {
                // fail调用接口失败
                reject({
                    errormsg: '网络错误,请稍后重试',
                    code: -1
                });
            }
        })
    });
    return promise; //注意，这里返回的是promise对象
}

let predict = (data) => {
    return new Promise((resolve, reject) => {
        resolve(apiRequest(apiList.predict, 'POST', data))
    })
}

let getOpenId = (data) => {
  return new Promise((resolve, reject) => {
      resolve(apiRequest(apiList.getOpenId, 'GET', data))
  })
}

let volumeAppLogin = (data) => {
  return new Promise((resolve, reject) => {
      resolve(apiRequest(apiList.volumeAppLogin, 'POST', data))
  })
}

let volumeUser = (data) => {
  return new Promise((resolve, reject) => {
      resolve(apiRequest(apiList.volumeUser, 'POST', data))
  })
}

//最后需要将具体调用的函数暴露出，给具体业务调用

export default {
  predict: predict, 
  getOpenId: getOpenId,
  volumeAppLogin: volumeAppLogin,
  volumeUser: volumeUser,
}